package com.netdisk.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import com.netdisk.dao.Dao;
import com.netdisk.entity.Log;

/**
 * 日志类
 * 
 * @author chenlong 2013-4-13
 */
@Repository(value = "logDao")
public class LogDao extends Dao<Log> {

	@Override
	public void save(Log obj) {
		Session session = sessionFactory.getCurrentSession();
		session.save(obj);
	}

	@Override
	public void update(Log obj) {
		Session session = sessionFactory.getCurrentSession();
		session.update(obj);
	}

	@Override
	public void delete(Log obj) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(obj);
	}

	@Override
	public List<Log> findAll() {
		Session session = sessionFactory.getCurrentSession();
		List<Log> logs = null;
		logs = session.createQuery("from Log").list();
		return logs;
	}

	@Override
	public Log findById(String id) {
		Session session = sessionFactory.getCurrentSession();
		List<Log> logs = null;
		Log log = null;
		Query query = session.createQuery("from Log where id=:id");
		query.setString("id", id);
		logs = query.list();
		if (!logs.isEmpty()) {
			log = logs.get(0);
		}
		return log;
	}

	@Override
	public List<Log> findByTop(int from, int max) {
		Session session = sessionFactory.getCurrentSession();
		List<Log> logs = null;
		Query query = session.createQuery("from Log");
		query.setFirstResult(from);
		query.setMaxResults(max);
		logs = query.list();
		return logs;
	}
}
